using System;
using System.Data;
using System.Runtime.Serialization;
using System.Xml;
using System.Collections;
using System.Globalization;
using System.ComponentModel;
namespace Deloitte.Data
{
	/// <summary>
	/// Summary description for DataTable2Collection.
	/// </summary>
	public class DataTable2Collection : InternalDataCollectionBase
	{
		private CollectionChangeEventHandler onCollectionChangedDelegate;
		private CollectionChangeEventHandler onCollectionChangingDelegate;		
		public event CollectionChangeEventHandler CollectionChanging = null;
 
		private DataSet2 dataSet;
		private ArrayList list;
		//private int defaultNameIndex;
		public DataTable2Collection(DataSet2 dataSet)
		{
			this.dataSet = null;
			this.list = new ArrayList();
			//this.defaultNameIndex = 1;
			this.onCollectionChangedDelegate = null;
			this.onCollectionChangingDelegate = null;
			this.dataSet = dataSet;			
		}
		public virtual DataTable Add()
		{
			DataTable2 table1 = new DataTable2();
			this.Add(table1);
			return table1;
		}
		public virtual void Add(DataTable2 table)
		{
			//this.OnCollectionChanging(new CollectionChangeEventArgs(CollectionChangeAction.Add, table));
			//this.BaseAdd(table);
			this.ArrayAdd(table);
			//this.OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, table));
		}
		public virtual DataTable Add(string name)
		{
			DataTable2 table1 = new DataTable2();
            table1.TableName = name;
			this.Add(table1);
			return table1;
		}
        public virtual DataTable Add(string csvFile, bool hasHeaders)
        {
            DataTable2 table1 = new DataTable2(csvFile, hasHeaders);
            this.Add(table1);
            return table1;
        }
		private void ArrayAdd(DataTable2 table)
		{
			this.List.Add(table);
		}
		public void AddRange(DataTable2[] tables)
		{
			/*if (this.dataSet.fInitInProgress)
			{
				this.delayedAddRangeTables = tables;
			}
			else if (tables != null)
			{*/
				DataTable2[] tableArray1 = tables;
				for (int num1 = 0; num1 < tableArray1.Length; num1++)
				{
					DataTable2 table1 = tableArray1[num1];
					if (table1 != null)
					{
						this.Add(table1);
					}
				}
			//}
		}
		public bool CanRemove(DataTable2 table)
		{
			return this.CanRemove(table, false);
		}
 
		internal bool CanRemove(DataTable table, bool fThrowException)
		{
			if (table == null)
			{
//				if (fThrowException)
//				{
//					throw ExceptionBuilder.ArgumentNull("table");
//				}
				return false;
			}
			if (table.DataSet != this.dataSet)
			{
//				if (fThrowException)
//				{
//					throw ExceptionBuilder.TableNotInTheDataSet(table.TableName);
//				}
				return false;
			}
			//this.dataSet.OnRemoveTableHack(table);
			if ((table.ChildRelations.Count != 0) || (table.ParentRelations.Count != 0))
			{
//				if (fThrowException)
//				{
//					throw ExceptionBuilder.TableInRelation();
//				}
				return false;
			}
			/*ParentForeignKeyConstraintEnumerator enumerator1 = new ParentForeignKeyConstraintEnumerator(this.dataSet, table);
			while (enumerator1.GetNext())
			{
				ForeignKeyConstraint constraint1 = enumerator1.GetForeignKeyConstraint();
//				if (fThrowException)
//				{
//					throw ExceptionBuilder.TableInConstraint(table, constraint1);
//				}
				return false;
			}
			ChildForeignKeyConstraintEnumerator enumerator2 = new ChildForeignKeyConstraintEnumerator(this.dataSet, table);
			while (enumerator2.GetNext())
			{
				ForeignKeyConstraint constraint2 = enumerator2.GetForeignKeyConstraint();
//				if (fThrowException)
//				{
//					throw ExceptionBuilder.TableInConstraint(table, constraint2);
//				}
//				return false;
			}*/
			return true;
		}



		protected override ArrayList List
		{
			get
			{
				return this.list;
			}
		}
		public void Clear()
		{
			int num1 = this.List.Count;
			DataTable2[] tableArray1 = new DataTable2[this.List.Count];
			this.List.CopyTo(tableArray1, 0);
			this.OnCollectionChanging(new CollectionChangeEventArgs(CollectionChangeAction.Remove, this.List));
//			if (this.dataSet.fInitInProgress && (this.delayedAddRangeTables != null))
//			{
//				this.delayedAddRangeTables = null;
//			}
			//this.BaseGroupSwitch(tableArray1, num1, null, 0);
			this.List.Clear();
			this.OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, this.List));			
		}
		public bool Contains(string name)
		{
			return (this.InternalIndexOf(name) >= 0);
		}
 
		internal int InternalIndexOf(string tableName)
		{
			int num1 = -1;
			if ((tableName != null) && (0 < tableName.Length))
			{
				int num2 = this.List.Count;
				int num3 = 0;
				for (int num4 = 0; num4 < num2; num4++)
				{
					DataTable table1 = (DataTable) this.List[num4];
					num3 = this.NamesEqual(table1.TableName, tableName, false, this.dataSet.Locale);
					if (num3 == 1)
					{
						return num4;
					}
					if (num3 == -1)
					{
						num1 = (num1 == -1) ? num4 : -2;
					}
				}
			}
			return num1;
		}
		internal int NamesEqual(string s1, string s2, bool fCaseSensitive, CultureInfo locale)
		{
			if (fCaseSensitive)
			{
				if (string.Compare(s1, s2, false, locale) == 0)
				{
					return 1;
				}
				return 0;
			}
			if (locale.CompareInfo.Compare(s1, s2, CompareOptions.IgnoreWidth | (CompareOptions.IgnoreKanaType | CompareOptions.IgnoreCase)) != 0)
			{
				return 0;
			}
			if (string.Compare(s1, s2, false, locale) == 0)
			{
				return 1;
			}
			return -1;
		}
 
		public virtual int IndexOf(DataTable2 table)
		{
			int num1 = this.List.Count;
			for (int num2 = 0; num2 < num1; num2++)
			{
				if (table == ((DataTable2) this.List[num2]))
				{
					return num2;
				}
			}
			return -1;
		}
		public virtual int IndexOf(string tableName)
		{
			int num1 = this.InternalIndexOf(tableName);
			if (num1 >= 0)
			{
				return num1;
			}
			return -1;
		}

		protected virtual void OnCollectionChanged(CollectionChangeEventArgs ccevent)
		{
			if (this.onCollectionChangedDelegate != null)
			{
				this.onCollectionChangedDelegate(this, ccevent);
			}
		}
 
		protected internal virtual void OnCollectionChanging(CollectionChangeEventArgs ccevent)
		{
			if (this.onCollectionChangingDelegate != null)
			{
				this.onCollectionChangingDelegate(this, ccevent);
			}
		}
 
		public void Remove(DataTable2 table)
		{
			this.OnCollectionChanging(new CollectionChangeEventArgs(CollectionChangeAction.Remove, table));
//			this.BaseRemove(table);
//			this.ArrayRemove(table);
			if (this.CanRemove(table))this.List.Remove(table);
			this.OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, table));
		}
		public void Remove(string name)
		{
			DataTable2 table1 = this[name];
			if (table1 == null)
			{
				throw new DataException("Table: " + name + " is not in the dataset");
			}
			this.Remove(table1);
		}
 
		public void RemoveAt(int index)
		{
			DataTable2 table1 = (DataTable2)this[index];
			if (table1 == null)
			{
				throw new DataException("The table index is out of the range");
			}
			this.Remove(table1);
		}
 
		public DataTable2 this[string name]
		{
			get
			{
				int num1 = this.InternalIndexOf(name);
				if (num1 == -2)
				{
					throw new Exception("CaseInsensitiveNameConflict" + name);
				}
				if (num1 >= 0)
				{
					return (DataTable2) this.List[num1];
				}
				return null;
			}
		}
 
		internal DataTable2 this[string name, string ns]
		{
			get
			{
				for (int num1 = 0; num1 < this.List.Count; num1++)
				{
					DataTable2 table1 = (DataTable2) this.List[num1];
					if ((table1.TableName == name) && (table1.Namespace == ns))
					{
						return table1;
					}
				}
				return null;
			}
		}
		public DataTable this[int index]
		{
			get
			{
				if ((index < 0) || (index >= this.List.Count))
				{
					throw new DataException("The table index is out of the range");
				}
				return (DataTable2) this.List[index];
			}
		}
 

		
	}
}
